import { createRouter, createWebHashHistory } from 'vue-router'
import { useUserStore } from '@/stores'
const router = createRouter({
  history: createWebHashHistory(import.meta.env.BASE_URL),
  routes: [
    { path: '/login', component: () => import('@/views/login/LoginPage.vue') }, // 登录页
    {
      path: '/',
      component: () => import('@/views/layout/LayoutContainer.vue'),
      redirect: '/login',
      children: [
        {
          path: '/product/productindex',
          component: () => import('@/views/product/ProductIndex.vue')
        },
        {
          path: '/equipment/equipmentSetParameter',
          component: () => import('@/views/equipment/equipmentSetParameter.vue')
        },
        {
          path: '/equipment/equipmentSwitchDetection',
          component: () => import('@/views/equipment/equipmentSwitchDetection.vue')
        },
        {
          path: '/equipment/equipmentList',
          component: () => import('/src/views/equipment/equipmentlist.vue')
        },
        {
          path: '/cloth/clothindex',
          component: () => import('@/views/cloth/ClothIndex.vue')
        },
        {
          path: '/cloth/countdefect',
          component: () => import('@/views/cloth/CountDefect.vue')
        },
        {
          path: '/cloth/quartilesystem',
          component: () => import('@/views/cloth/quartileSystem.vue')
        },
        {
          path: '/user/password',
          component: () => import('@/views/user/UserPassword.vue')
        },
        {
          path: '/user/permission',
          component: () => import('@/views/user/PermissionManagement.vue')
        },
        {
          path: '/ota/otamanagement',
          component: () => import('@/views/OTA/otaManagement.vue')
        },
        {
          path: '/ota/otalist',
          component: () => import('@/views/OTA/otaList.vue')
        }
      ]
    },
    {
      path: '/qrcode',
      component: () => import('@/views/qrcode/qrLayout.vue'),
      meta: { requiresAuth: false } // 标记不需要登录
    }
  ]
})
router.beforeEach((to) => {
  // 如果没有token, 且访问的是非登录页，拦截到登录，其他情况正常放行
  const useStore = useUserStore()
  if (!useStore.token && to.path !== '/login' && to.meta.requiresAuth !== false) return '/login'
})
export default router
